use serde::{Deserialize, Serialize};
use tauri::AppHandle;
use std::sync::Arc;
use std::fmt;
#[derive(Clone, Serialize, Deserialize)]
pub struct ServerConfig {
    pub host: String,
    pub port: u16,
    pub thread_count: u16,
    #[serde(skip)]
    pub message_handler: Option<Arc<dyn Fn(Message) + Send + Sync>>
}
impl fmt::Debug for ServerConfig {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("ServerConfig")
            .field("host", &self.host)
            .field("port", &self.port)
            .field("thread_count", &self.thread_count)
            // 处理 message_handler 字段，避免调用其 Debug
            .field(
                "message_handler",
                &self.message_handler.as_ref().map(|_|
                    // 无法获取闭包具体内容，仅显示存在与否
                    "Some(closure)"
                ).unwrap_or("None"),
            )
            .finish()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Message {
    Start { timestamp: String },
    Data { timestamp: String, topic: String, content: String },
    Stop { timestamp: String }
}

// 定义数据处理函数
fn data_task(topic: String, content: String, _app_handle: AppHandle) {
    log::info!("Processing data for topic: {}", topic);
    log::debug!("Content: {}", content);
    // 这里可以添加实际的数据处理逻辑
}

pub fn create_message_queue_server_config(app_handle: AppHandle) -> ServerConfig {
    ServerConfig {
        host: "127.0.0.1".to_string(),
        port: 8080,
        thread_count: 4,
        message_handler: Some(Arc::new(move |message| {
            match message {
                Message::Start { timestamp } => {
                    println!("Server: Received Start message at {}", timestamp);
                    println!("Server: Executing Start Task...");
                }
                Message::Data { timestamp, topic, content } => {
                    println!("Server: Received Data message at {}", timestamp);
                    data_task(topic, content, app_handle.clone());
                }
                Message::Stop { timestamp } => {
                    println!("Server: Received Stop message at {}", timestamp);
                    println!("Server: Executing Stop Task...");
                }
            }
        })),
    }
}
